package com.ddtech.framework.system.rest;

import com.ddtech.config.DataScope;
import com.ddtech.exception.BadRequestException;
import com.ddtech.framework.log.aop.log.Log;
import com.ddtech.framework.log.service.LoginLogService;
import com.ddtech.framework.system.model.SysDept;
import com.ddtech.framework.system.service.DeptService;
import com.ddtech.framework.system.service.dto.DeptDTO;
import com.ddtech.framework.system.service.dto.DeptQueryCriteria;
import com.ddtech.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Apache License
 * @date 2019-03-25
 */
@Api(tags = "系统-部门管理")
@RestController
@RequestMapping("api")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private LoginLogService loginLogService;

    @Autowired
    private DataScope dataScope;

    private static final String ENTITY_NAME = "dept";

    @Log("查询部门")
    @ApiOperation("查询部门")
    @GetMapping(value = "/dept")
    @PreAuthorize("hasAnyRole('ADMIN','USER_ALL','USER_SELECT','DEPT_ALL','DEPT_SELECT')")
    public ResponseEntity getDepts(DeptQueryCriteria criteria) {
        // 数据权限
        criteria.setIds(dataScope.getDeptIds());
        List<DeptDTO> deptDTOS = deptService.queryAll(criteria);
        return new ResponseEntity(deptService.buildTree(deptDTOS), HttpStatus.OK);
    }

    @Log("新增部门")
    @ApiOperation("新增部门")
    @PostMapping(value = "/dept")
    @PreAuthorize("hasAnyRole('ADMIN','DEPT_ALL','DEPT_CREATE')")
    public ResponseEntity create(@Validated @RequestBody SysDept resources) {
        if (resources.getId() != null) {
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        return new ResponseEntity(deptService.create(resources), HttpStatus.CREATED);
    }

    @Log("修改部门")
    @ApiOperation("修改部门")
    @PutMapping(value = "/dept")
    @PreAuthorize("hasAnyRole('ADMIN','DEPT_ALL','DEPT_EDIT')")
    public ResponseEntity update(@Validated(SysDept.Update.class) @RequestBody SysDept resources) {
        deptService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除部门")
    @ApiOperation("删除部门")
    @DeleteMapping(value = "/dept/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','DEPT_ALL','DEPT_DELETE')")
    public ResponseEntity delete(@PathVariable String id) {
        loginLogService.deleteByDeptId(id);
        deptService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation("根据部门ID查询部门树")
    @GetMapping(value = "/dept/findDepTree")
    public ResponseEntity findDepTreeById(String deptId) {
        if (!StringUtils.isEmpty(deptId)) {
            List<DeptDTO> deptDTOS = deptService.findDepTreeById(deptId);
            return new ResponseEntity(deptService.buildTree(deptDTOS), HttpStatus.OK);
        }
        return new ResponseEntity(HttpStatus.OK);
    }
}